package com.hyl.controller;

import cn.hutool.core.util.ObjectUtil;
import com.hyl.bean.User;
import com.hyl.common.ResponseData;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {

    /**
     * 模拟登录，并存储用户信息到session中
     * @param user
     * @param httpSession
     * @return
     */
    @PostMapping("/login")
    public ResponseData<User> login(@RequestBody User user, HttpSession httpSession){
        //简单登录，不访问数据库
        if(ObjectUtil.isNull(user) || !"123456".equals(user.getPassword())){
            return ResponseData.fail("用户名或密码错误");
        }
        //登录成功，将用户名保存到session中
        httpSession.setAttribute("currentUser", user.getUsername());
        return ResponseData.success(user);
    }

    /**
     * 获取当前登录用户名
     * @param httpSession
     * @return
     */
    @GetMapping("/getUsername")
    public ResponseData<String> getUsername(HttpSession httpSession){
        return ResponseData.success((String)httpSession.getAttribute("currentUser"));
    }
}
